// Package database @program: rankinglist
//@author: edte
//@create: 2021-12-29 15:45
package database

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/go-redis/redis/v8"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"log"
	log2 "rankinglist/log"
	"rankinglist/tools"
)

type Scoreboards struct {
	Id       int `gorm:"primary_key"`
	Username string
	Score    int
}

type GameScores struct {
	Id               int
	PlaygroundNumber int
	NickName         string
	Wins             int
	Odds             float64
}

func MigrationDataToMysql() error {
	//log.Println("今日程序")
	mysqlPath := tools.GetEnv("MYSQL_PATH", "localhost:3306")
	mysqlUser := tools.GetEnv("MYSQL_USER", "root")
	mysqlPassword := tools.GetEnv("MYSQL_PASSWORD", "123456")
	db, err := gorm.Open(mysql.Open(mysqlUser+":"+mysqlPassword+"@tcp("+mysqlPath+")/gamedata?charset=utf8&parseTime=True&loc=Local"), &gorm.Config{})
	if err != nil {
		log.Println(err.Error())
		writeLog, err2 := log2.WriteLog(context.Background(), "ranklist", "MigrationDataToMysql", "MigrationData", "Error", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		return err
	}
	//err = db.AutoMigrate(&Scoreboards{})
	//db, err := gorm.Open(mysql.Open("root:123456@tcp(192.168.88.128:3306)/gamedata?charset=utf8&parseTime=True&loc=Local"), &gorm.Config{})

	err = db.AutoMigrate(&GameScores{})
	if err != nil {
		log.Println(err.Error())

		writeLog, err2 := log2.WriteLog(context.Background(), "ranklist", "MigrationDataToMysql", "MigrationData", "Error", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}

		return err
	}
	var board []GameScores
	db.Model(GameScores{}).Find(&board)

	/*var board []Scoreboards
	db.Model(Scoreboards{}).Find(&board)
	*/
	redisPath := tools.GetEnv("REDIS_PATH", "localhost:6379")
	redisPD := tools.GetEnv("REDIS_PASSWORD", "")
	rdb := redis.NewClient(&redis.Options{
		Addr:     redisPath,
		Password: redisPD, // no password set
		DB:       4,       // use default DB
		//PoolSize: 500, // 连接池大小
	})

	defer func(rdb *redis.Client) {
		err := rdb.Close()
		if err != nil {
			log.Println(err)
			writeLog, err2 := log2.WriteLog(context.Background(), "ranklist", "MigrationDataToMysql", "MigrationData", "Warring", err.Error())
			if writeLog == 200 {
				fmt.Println("成功写入日志")
			}
			if err2 != nil {
				fmt.Println("写入日志失败")
			}
		}
	}(rdb)
	cxt := context.Background()

	for _, value := range board {
		tmpBoard := Scoreboards{
			Id:       value.Id,
			Username: value.NickName,
		}
		tmpBoard.Score = value.PlaygroundNumber
		marshal, err := json.Marshal(tmpBoard)
		if err != nil {
			log.Println("Data migration error")
			writeLog, err2 := log2.WriteLog(context.Background(), "ranklist", "MigrationDataToMysql", "MigrationData", "Warring", err.Error())
			if writeLog == 200 {
				fmt.Println("成功写入日志")
			}
			if err2 != nil {
				fmt.Println("写入日志失败")
			}
			return err
		}
		rdb.ZAdd(cxt, "playGrountNum", &redis.Z{Score: float64(value.PlaygroundNumber), Member: marshal})
		tmpBoard.Score = value.Wins
		marshal, err = json.Marshal(tmpBoard)
		if err != nil {
			writeLog, err2 := log2.WriteLog(context.Background(), "ranklist", "MigrationDataToMysql", "MigrationData", "Warring", err.Error())
			if writeLog == 200 {
				fmt.Println("成功写入日志")
			}
			if err2 != nil {
				fmt.Println("写入日志失败")
			}
			log.Println("Data migration error")
			return err
		}
		rdb.ZAdd(cxt, "WinNum", &redis.Z{Score: float64(value.Wins), Member: marshal})
		tmpBoard.Score = int(value.Odds * 100)
		marshal, err = json.Marshal(tmpBoard)
		if err != nil {
			writeLog, err2 := log2.WriteLog(context.Background(), "ranklist", "MigrationDataToMysql", "MigrationData", "Warring", err.Error())
			if writeLog == 200 {
				fmt.Println("成功写入日志")
			}
			if err2 != nil {
				fmt.Println("写入日志失败")
			}
			log.Println("Data migration error")
			return err
		}
		rdb.ZAdd(cxt, "playOdds", &redis.Z{Score: value.Odds, Member: marshal})
	}
	writeLog, err2 := log2.WriteLog(context.Background(), "ranklist", "MigrationDataToMysql", "MigrationData", "Debug", "Data migration succeeded")
	if writeLog == 200 {
		fmt.Println("成功写入日志")
	}
	if err2 != nil {
		fmt.Println("写入日志失败")
	}
	return err
}
